package com.ruoyi.yard.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.base.util.GenCodeUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.generator.util.GenUtils;
import com.ruoyi.yard.domain.*;
import com.ruoyi.yard.mapper.CompWarehouseInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.yard.mapper.WaybillInfoMapper;
import com.ruoyi.yard.service.IWaybillInfoService;

/**
 * 发运单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-05
 */
@Service
public class WaybillInfoServiceImpl implements IWaybillInfoService 
{
    @Autowired
    private WaybillInfoMapper waybillInfoMapper;

    @Autowired
    private CompWarehouseInfoMapper compWarehouseInfoMapper;
    /**
     * 查询发运单
     * 
     * @param id 发运单主键
     * @return 发运单
     */
    @Override
    public WaybillInfo selectWaybillInfoById(Long id)
    {
        WaybillInfo waybillInfo = waybillInfoMapper.selectWaybillInfoById(id);
        List<CompWarehouseInfo>compWarehouseInfoList=compWarehouseInfoMapper.selectCompWarehouseInfoByWaybillNo(waybillInfo.getWaybillNo());
        waybillInfo.setCompWarehouseInfoList(compWarehouseInfoList);

        return waybillInfo;
    }

    /**
     * 查询发运单列表
     * 
     * @param waybillInfo 发运单
     * @return 发运单
     */
    @Override
    public List<WaybillInfo> selectWaybillInfoList(WaybillInfo waybillInfo)
    {
        List<WaybillInfo> waybillInfoList = waybillInfoMapper.selectWaybillInfoList(waybillInfo);

        return waybillInfoList;
    }

    /**
     * 新增发运单
     * 
     * @param waybillInfo 发运单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWaybillInfo(WaybillInfo waybillInfo)
    {
        waybillInfo.setWaybillNo(GenCodeUtils.getWaybillNo());
        int rows = waybillInfoMapper.insertWaybillInfo(waybillInfo);
        insertWaybillCompRef(waybillInfo);
        return rows;
    }

    /**
     * 修改发运单
     * 
     * @param waybillInfo 发运单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWaybillInfo(WaybillInfo waybillInfo)
    {
        WaybillInfo waybillInfo1 = waybillInfoMapper.selectWaybillInfoById(waybillInfo.getId());
        if (waybillInfo1.getApprovalStatus() != 0)
            throw new ServiceException("发运单已审核,无法修改");

        waybillInfoMapper.deleteWaybillCompRefByWarehouseCompomentNo(waybillInfo.getId());
        insertWaybillCompRef(waybillInfo);
        return waybillInfoMapper.updateWaybillInfo(waybillInfo);
    }

    /**
     * 批量删除发运单
     * 
     * @param ids 需要删除的发运单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWaybillInfoByIds(Long[] ids)
    {
        waybillInfoMapper.deleteWaybillCompRefByWarehouseCompomentNos(ids);
        return waybillInfoMapper.deleteWaybillInfoByIds(ids);
    }

    /**
     * 删除发运单信息
     * 
     * @param id 发运单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWaybillInfoById(Long id)
    {
        WaybillInfo waybillInfo1 = waybillInfoMapper.selectWaybillInfoById(id);

        if (waybillInfo1==null)
            throw new ServiceException("发运单不存在");
        if (waybillInfo1.getApprovalStatus() != 0)
            throw new ServiceException("发运单已审核,无法删除");

        waybillInfoMapper.deleteWaybillCompRefByWarehouseCompomentNo(id);
        return waybillInfoMapper.deleteWaybillInfoById(id);
    }

    @Override
    public List<WaybillInfo> selectHomePageWaybillInfoList(WaybillInfo waybillInfo) {
        List<WaybillInfo> waybillInfoList = waybillInfoMapper.selectHomePageWaybillInfoList(waybillInfo);
        for (WaybillInfo w:waybillInfoList){
            List<CompWarehouseInfo>compWarehouseInfoList=compWarehouseInfoMapper.selectCompWarehouseInfoByWaybillNo(w.getWaybillNo());
            w.setCompWarehouseInfoList(compWarehouseInfoList);
            Long count=0L;
            Double weight=0D;
            for (CompWarehouseInfo compWarehouseInfo:compWarehouseInfoList){
                count=count+compWarehouseInfo.getNeedCount();
                weight=weight+compWarehouseInfo.getNetWeight();
            }
            w.setTotalWeight(weight);
            w.setTotalCount(count);
        }
        return waybillInfoList;
    }

    @Override
    public int toExamine(JSONObject params) {
        Long id = params.getLong("id");
        //审批状态(0未审核 1通过 2驳回)
        Integer state = params.getInteger("state");

        if (StringUtils.isNull(id) || StringUtils.isNull(state))
            throw new ServiceException("参数错误！") ;
        WaybillInfo waybillInfo = waybillInfoMapper.selectWaybillInfoById(id);
        if (waybillInfo==null)
            throw new ServiceException("发货单不存在！") ;
        if (waybillInfo.getApprovalStatus()!=0){
            throw new ServiceException("发货计划已审核") ;
        }
        WaybillInfo w=new WaybillInfo();
        w.setId(id);
        w.setApprovalStatus(state);
        return waybillInfoMapper.updateWaybillInfo(w);
    }

    @Override
    public int printWaybill(JSONObject params) {
        Long id = params.getLong("id");
        WaybillInfo waybillInfo = waybillInfoMapper.selectWaybillInfoById(id);
        if (waybillInfo.getApprovalStatus()==0){
            throw new ServiceException("发货计划未审核") ;
        }
        if (waybillInfo.getApprovalStatus()==2)
            throw new ServiceException("发货计划审核未通过") ;
        WaybillInfo w=new WaybillInfo();
        w.setId(id);
        w.setIsPrint(1);
        return waybillInfoMapper.updateWaybillInfo(w);
    }

    /**
     * 新增发运单_构件信息
     * 
     * @param waybillInfo 发运单对象
     */
    public void insertWaybillCompRef(WaybillInfo waybillInfo)
    {
        List<WaybillCompRef> waybillCompRefList = waybillInfo.getWaybillCompRefList();
        Long id = waybillInfo.getId();
        if (StringUtils.isNotNull(waybillCompRefList))
        {
            List<WaybillCompRef> list = new ArrayList<WaybillCompRef>();
            for (WaybillCompRef waybillCompRef : waybillCompRefList)
            {
                waybillCompRef.setWaybillInfoNo(waybillInfo.getWaybillNo());
                list.add(waybillCompRef);
            }
            if (list.size() > 0)
            {
                waybillInfoMapper.batchWaybillCompRef(list);
            }
        }
    }
}
